package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Popular;
import com.sqx.modules.health.service.IPopularService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 健康科普表 前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-06-14
 */

@Api(value = "健康科普表", tags = "健康科普表", description = "")
@RestController
@RequestMapping("/health/popular")
public class PopularController {
    @Autowired
    private IPopularService popularService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新健康科普表", notes = "新增或更新健康科普表")
    public Result save(@RequestBody Popular popular) {
        return Result.success(popularService.saveOrUpdate(popular));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除健康科普表", notes = "根据ID删除健康科普表")
    public Result delete(@PathVariable Integer id) {
        return Result.success(popularService.removeById(id));
    }

    // 根据IDS删除
    @GetMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除健康科普表", notes = "根据IDS删除健康科普表")
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        return Result.success(popularService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部健康科普表", notes = "查询全部健康科普表")
    public Result findAll() {
        return Result.success(popularService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录健康科普表", notes = "根据ID查询一条记录健康科普表")
    public Result findOne(@RequestParam Integer id) {
        return Result.success(popularService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询健康科普表(小程序查看更多兼容)", notes = "分页查询健康科普表(小程序查看更多兼容)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", paramType = "String"),
            @ApiImplicitParam(name = "hid", value = "医院id", paramType = "decimal"),
            @ApiImplicitParam(name = "isable", value = "是否启用 0：启用  1：禁用", paramType = "Integer"),
            @ApiImplicitParam(name = "title", value = "标题 --> 模糊查询", paramType = "string"),
    })
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") Integer hid,
                           @RequestParam(required = false, defaultValue = "") Integer isable,
                           @RequestParam(required = false, defaultValue = "") String title) {
        QueryWrapper<Popular> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(hid)) {
            queryWrapper.eq("hid", hid);
        }
        if (ObjectUtil.isNotEmpty(isable)) {
            queryWrapper.eq("isable", isable);
        }
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.orderByDesc("id");
        return Result.success(popularService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 小程序首页展示 展示推荐并且启动状态下的所有数据
     *
     * @param hid
     * @return
     */
    @GetMapping("/homeDisplay")
    @ApiOperation(value = "小程序首页展示", notes = "小程序首页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hid", value = "医院id", paramType = "decimal"),
    })
    public Result homeDisplay(@RequestParam(required = false, defaultValue = "") Long hid) {
        QueryWrapper<Popular> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("isable", 0);
        queryWrapper.eq("isrec", 0);
        queryWrapper.eq("hid", hid);
        List<Popular> result = null;
        List<Popular> populars = popularService.list(queryWrapper);
        if (populars.size() != 0) {
            result = populars;
        } else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");
            queryWrapper.eq("isable", 0);
            queryWrapper.eq("hid", hid);
            result = popularService.list(queryWrapper);
        }
        return Result.success(result);
    }

    @ApiOperation("获取健康数据,医护端首页轮播")
    @GetMapping("getRandom")
    public Result getRandom(@RequestParam(required = false, defaultValue = "") Long hid){
        QueryWrapper<Popular> wrapper = new QueryWrapper<>();

        if (ObjectUtil.isEmpty(hid)) {
            return Result.error(Constants.CODE_500, "参数错误");
        }
        wrapper.eq("hid", hid);
        wrapper.eq("isable", 0);

        List<Popular> list = popularService.list(wrapper);
        if (ObjectUtil.isEmpty(list)) {
            return Result.error(Constants.CODE_500, "暂无可用数据");
        }
//        Random random = new Random();
//        .get(random.nextInt(list.size()))
        return Result.success(list);
    }


}

